package cn.dianchiyun.protocol.netty.dtu.handler.impl;

import cn.dianchiyun.protocol.Const;
import cn.dianchiyun.protocol.netty.dtu.entity.DTUWrite;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DTUHandler extends SimpleChannelInboundHandler<Object> {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public void messageReceived(ChannelHandlerContext ctx, Object message) {
		logger.info("服务器发送的数据:{}", message.toString());
		if(message!=null && StringUtils.isNotBlank(message.toString())) {
			//String msg = message.toString();
			//if (cn.dianchiyun.protocol.Const.HEART_STR.equals(msg)) {
				logger.info("心跳。。。");
				//ctx.channel().writeAndFlush(Const.OK);
				//String cmd = "~1001410000AAAA";
				//String cmd = "14000\np\r";


				ctx.channel().writeAndFlush(new DTUWrite("01", "04", 0, 10));
			//}/**/ else if(msg.length() >= 6) {
				//String type = msg.substring(0,2);
			//}
		}
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception{
		logger.info("channelActive>>>>>>>>已连接");
		super.channelActive(ctx);
		logger.info("ctx.name():"+ctx.name());
		logger.info("ctx.channel().id():"+ctx.channel().id());
		logger.info("ctx.channel().localAddress():"+ctx.channel().localAddress());
		logger.info("ctx.channel().remoteAddress():"+ctx.channel().remoteAddress());
		//logger.info("ctx.channel().remoteAddress():"+ctx.channel().remoteAddress().getClass());

        /*InetSocketAddress inetSocketAddress = (InetSocketAddress)ctx.channel().remoteAddress();
        logger.info("ctx.channel().remoteAddress():"+inetSocketAddress.getHostString());
        logger.info("ctx.channel().remoteAddress():"+inetSocketAddress.getPort());
        logger.info("ctx.channel().remoteAddress():"+inetSocketAddress.getAddress());
        logger.info("ctx.channel().remoteAddress():"+inetSocketAddress.getHostName());*/
		//ChannelManager.getInstance().addChannel(ctx.channel().remoteAddress().toString(), ctx.channel());
	}

	/**
	 * 客户端关闭的处理
	 * @param ctx
	 * @throws Exception
	 */
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		logger.info("channelInactive>>>>>>>>已断开连接");
		super.channelInactive(ctx);
		//ChannelManager.getInstance().remove(ctx.channel());
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		logger.info("已经"+ Const.TIMEOUT+"秒未收到客户端的消息了！");
		if (evt instanceof IdleStateEvent){
			IdleStateEvent event = (IdleStateEvent)evt;
			if (event.state() == IdleState.READER_IDLE){
				logger.error("关闭这个不活跃通道！");
				ctx.channel().close();
			}
		}else {
			super.userEventTriggered(ctx, evt);
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		ctx.close();
	}

}
